package com.shoujifeng.snowmusic.player;

import org.json.JSONException;

import com.shoujifeng.snowmusic.player.R;
import com.shoujifeng.snowmusic.player.http.ServerAccess;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 注册
 * @author luo
 *
 */

public class RegisterActivity extends Activity{
	private Button breakBtn;
	private Button getCheckingBtn;
	private EditText numberEdt;
	private ServerAccess serverAccess;
	private Thread accThread;
	private final int UPDATE_DATA = 0;
	private final int UPDATE_ONE  = 1;
	private String code;
	private String code_type;
	private EditText passwordEdt;
	private EditText verifyPasswordEdt;
	private Button registerBtn;
	private EditText authCode;
	private String authOrRegister = "";
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		init();
	}
	private void init(){
		serverAccess = new ServerAccess(RegisterActivity.this);
		breakBtn = (Button) findViewById(R.id.breakBtn);
		breakBtn.setOnClickListener(onClickListener);
		numberEdt = (EditText) findViewById(R.id.numberEdt);
		getCheckingBtn = (Button) findViewById(R.id.getCheckingBtn);
		getCheckingBtn.setOnClickListener(onClickListener);
		passwordEdt = (EditText) findViewById(R.id.passwordEdt);
		verifyPasswordEdt = (EditText) findViewById(R.id.verifyPasswordEdt);
		registerBtn = (Button) findViewById(R.id.registerBtn);
		registerBtn.setOnClickListener(onClickListener);
		authCode = (EditText) findViewById(R.id.authCode);
	}
	private View.OnClickListener onClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.breakBtn:{
				finish();
				overridePendingTransition(R.anim.left_in, R.anim.right_out);
			}
				break;
			case R.id.getCheckingBtn:{
				authOrRegister = "auth";
				if(numberEdt.getText().toString().equals("")){
					Toast.makeText(RegisterActivity.this, "请先输入手机号码!", Toast.LENGTH_LONG)
					.show();
				}
				else{
					Toast.makeText(RegisterActivity.this, "正在发送!", Toast.LENGTH_LONG)
					.show();
					accThread = new Thread(runnable);
					accThread.start();
				}
				
			}	
				break;
			case R.id.registerBtn:{
				if(passwordEdt.getText().toString().length()<6){
					Toast.makeText(RegisterActivity.this, "密码不能少于6位数!", Toast.LENGTH_SHORT)
					.show();
				}else
				if(!verifyPasswordEdt.getText().toString().equals(passwordEdt.getText().toString())){
					Toast.makeText(RegisterActivity.this, "两次输入的密码不一致!", Toast.LENGTH_SHORT)
					.show();
				}else
				if(authCode.getText().toString().equals("")){
					Toast.makeText(RegisterActivity.this, "验证码不能为空!", Toast.LENGTH_SHORT)
					.show();
				}else
					if(!authCode.getText().toString().equals(code)){
						Toast.makeText(RegisterActivity.this, "验证码不正确!", Toast.LENGTH_SHORT)
						.show();
				}
				else{
					authOrRegister = "register";
					accThread = new Thread(runnable);
					accThread.start();
				}
				
			}	
			default:
				break;
			}
		}
	};
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
		{
			finish();
			
			overridePendingTransition(R.anim.no_move, R.anim.right_out);
		}
			break;

		default:
			break;
		}
		
		return super.onKeyDown(keyCode, event);
	}
	
	protected Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case UPDATE_ONE: {
				if(code_type.equals("0")){
					Toast.makeText(RegisterActivity.this, "注册失败，请重试!", Toast.LENGTH_SHORT)
					.show();
				}
				if(code_type.equals("1")){
					Toast.makeText(RegisterActivity.this, "注册成功!", Toast.LENGTH_SHORT)
					.show();
					finish();
				}
				if(code_type.equals("2")){
					Toast.makeText(RegisterActivity.this, "该用户已存在!", Toast.LENGTH_SHORT)
					.show();
				}
			}
				break;
				
			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	// 创建多线程获取数据
	protected Runnable runnable = new Runnable() {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			try {
				if(authOrRegister.equals("auth")){
					code = serverAccess.getChecking(numberEdt.getText().toString());
					authOrRegister = "";
					sendMessage(UPDATE_DATA);
				}
				if(authOrRegister.equals("register")){
					code_type = serverAccess.register(numberEdt.getText().toString(), passwordEdt.getText().toString());
					authOrRegister = "";
					sendMessage(UPDATE_ONE);
				}
				
				

			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	};

	// 发送消息到Handler
	private void sendMessage(int what) {
		Message msg = new Message();
		msg.what = what;
		handler.sendMessage(msg);
	}
}
